//
//  ShowViewController.m
//  ArtOfLiving
//
//  Created by 刘虎 on 16/5/11.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "ShowViewController.h"
#import "GoOutGuideDetailViewController.h"
#import "MoviePlayerViewController.h"
#import "FoodDetailViewController.h"
#import "UITableView+EmptyData.h"
#import "FoodMainModel.h"

@interface ShowViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataSource;//存储收藏的数据

@end

@implementation ShowViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    if ([self.fromCELL isEqualToString:@"本地锦囊"]) {
        
        [self.dataSource addObjectsFromArray:[[DataBaseManager shareDataBaseManager] fetchGuideDataWithTitle:nil downloadURL:nil]];
    }
    if ([self.fromCELL isEqualToString:@"视频收藏"]) {
        
        [self.dataSource addObjectsFromArray:[[DataBaseManager shareDataBaseManager] fetchVideoDataWithTitle:nil url:nil descriptions:nil feed:nil]];
    }
    if ([self.fromCELL isEqualToString:@"美食收藏"]) {
        
        [self.dataSource addObjectsFromArray:[[DataBaseManager shareDataBaseManager] fetchFoodDataWithimageUrl:nil url:nil ID:nil]];
    }
    if ([self.fromCELL isEqualToString:@"本地视频"]) {
        
        NSArray *tmpArr = [[DataBaseManager shareDataBaseManager] fetchVideoDataWithTitle:nil url:nil descriptions:nil feed:nil];
        NSMutableArray *mArr = [[NSMutableArray alloc] init];
        for (CollectionVideo *model in tmpArr) {
            
            if (![model.url containsString:@"http"]) {
                
                [mArr addObject:model];
            }
        }
        [self.dataSource addObjectsFromArray:mArr];
    }
    if ([self.registerCELL isEqualToString:@"CollectionGuideCELL"]) {
        
        self.navigationItem.title = @"下载锦囊";
    }
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}


#pragma mark - ----tableView的代理方法----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [tableView showmessage:[NSString stringWithFormat:@"还没有✨艺点✨%@💧前去收藏",self.fromCELL] byDataSourceCount:self.dataSource.count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseCell *cell = [tableView dequeueReusableCellWithIdentifier:self.registerCELL forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSourceArr[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.registerCELL isEqualToString:@"CollectionGuideCELL"]) {
        
        DownloadGuide *model = [self.dataSource objectAtIndex:indexPath.row];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        NSArray *arr = [model.path componentsSeparatedByString:@"/"];
        path = [path stringByAppendingPathComponent:arr.lastObject];
        GoOutGuideDetailViewController *VC = [[GoOutGuideDetailViewController alloc] init];
        VC.jsonPath = path;
        VC.model = model;
        [self.navigationController pushViewController:VC animated:YES];
    } else if ([self.registerCELL isEqualToString:@"foodMainCELL"]) {
        
        FoodCollection *model = [self.dataSource objectAtIndex:indexPath.row];
        FoodDetailViewController *foodDetailVC = [[FoodDetailViewController alloc] init];
        foodDetailVC.Id = @([model.iD integerValue]);
        [self.navigationController pushViewController:foodDetailVC animated:YES];
    } else {
        
        
        MoviePlayerViewController *moviPlay = [[MoviePlayerViewController alloc] init];
        moviPlay.index = indexPath.row;
        CollectionVideo *model = [self.dataSource objectAtIndex:indexPath.row];
        moviPlay.url = model.url;
        moviPlay.flagStr = @"collectionVideo";
       [self.navigationController presentViewController:moviPlay animated:YES completion:nil];
    }
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.registerCELL isEqualToString:@"CollectionGuideCELL"]) {
        
        return 30;
    } else if ([self.registerCELL isEqualToString:@"CollectionVideoCELL"]) {
        
        return 300;
    } else if ([self.registerCELL isEqualToString:@"foodMainCELL"]) {
        
        return 200;
    }
    return 0.0f;
}



#pragma mark - ----------tableViewDelegate-------------
//让tableView进入可编辑模式
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

//设置tableView的编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //进入删除模式
    return UITableViewCellEditingStyleDelete;
}

//当tableViewCell编辑的时候执行的事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
  
        
        //删除数据库中的数据
        if ([self.fromCELL isEqualToString:@"本地锦囊"]) {
            DownloadGuide *model = self.dataSource[indexPath.row];
            
           [[DataBaseManager shareDataBaseManager] deleteGuideDataWithTitle:model.title downloadURL:model.downloadURL];
        }
        if ([self.fromCELL isEqualToString:@"视频收藏"]) {
            
            CollectionVideo *model = self.dataSource[indexPath.row];
            [[DataBaseManager shareDataBaseManager] deleteVideoDataWithTitle:model.title url:model.url descriptions:model.description feed:model.feed];
            [self.tableView reloadData];
        }
        if ([self.fromCELL isEqualToString:@"美食收藏"]) {
            
            FoodCollection *model = self.dataSource[indexPath.row];
            [[DataBaseManager shareDataBaseManager] deleteFoodDataWithimageUrl:model.imageUrl url:model.urlString ID:model.iD];
        }
        if ([self.fromCELL isEqualToString:@"本地视频"]) {
            
            CollectionVideo *model = self.dataSource[indexPath.row];
            [[DataBaseManager shareDataBaseManager] deleteVideoDataWithTitle:model.title url:model.url descriptions:model.description feed:model.feed];
        }
        //删除数据源
        [self.dataSource removeObjectAtIndex:indexPath.row];
        //删除UI(行)
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView reloadData];
    }
}

#pragma mark - ------属性懒加载------
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        [_tableView registerNib:[UINib nibWithNibName:self.cellName bundle:nil] forCellReuseIdentifier:self.registerCELL];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}
@end
